package com.zuipin.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zuipin.entity.product.ProductImg;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.exception.CustomException;
import com.zuipin.framework.result.Result;
import com.zuipin.mapper.ProductImgMapper;
import com.zuipin.service.IProductImgService;
import com.zuipin.util.DateUtils;
import com.zuipin.vo.ProductImgVo;

import net.sf.json.JSONObject;

/**
 * 商品相册信息业务类实现
 *
 * @ClassName ProductImgServiceImpl 
 * @author wangcongyi
 * @date 2017年6月9日 上午9:16:37
 */
@Service
public class ProductImgServiceImpl implements IProductImgService {
	
	@Resource
	private ProductImgMapper productImgMapper;
	
	@Override
	public List<ProductImgVo> selectByProExtId(JSONObject object) {
		// TODO Auto-generated method stub
		return productImgMapper.selectByProExtId(object);
	}
	
	@Override
	public List<ProductImgVo> selectByProExtIdBack(JSONObject object) {
		// TODO Auto-generated method stub
		return productImgMapper.selectByProExtIdBack(object);
	}
	
	@Override
	public List<ProductImg> findByProExtId(JSONObject object) {
		// TODO Auto-generated method stub
		return productImgMapper.findByProExtId(object);
	}
	
	@Override
	public int deleteByProExtIdAndSysId(Long proExtId, Long sysId) {
		// TODO Auto-generated method stub
		return productImgMapper.deleteByProExtIdAndSysId(proExtId, sysId);
	}
	
	@Override
	public List<ProductImg> getByProExtId(JSONObject object) {
		// TODO Auto-generated method stub
		return productImgMapper.getByProExtId(object);
	}
	
	@Override
	public int insertBatch(List<ProductImg> productImgs) {
		// TODO Auto-generated method stub
		return productImgMapper.insertBatch(productImgs);
	}
	
	@Override
	public int deleteByPrimaryKey(Long id) {
		// TODO Auto-generated method stub
		return productImgMapper.deleteByPrimaryKey(id);
	}
	
	@Override
	public int insert(ProductImg record) {
		// TODO Auto-generated method stub
		return productImgMapper.insert(record);
	}
	
	@Override
	public int insertSelective(ProductImg record) {
		// TODO Auto-generated method stub
		return productImgMapper.insertSelective(record);
	}
	
	@Override
	public ProductImg selectByPrimaryKey(Long id) {
		// TODO Auto-generated method stub
		return productImgMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public int updateByPrimaryKeySelective(ProductImg record) {
		// TODO Auto-generated method stub
		return productImgMapper.updateByPrimaryKeySelective(record);
	}
	
	@Override
	public int updateByPrimaryKey(ProductImg record) {
		// TODO Auto-generated method stub
		return productImgMapper.updateByPrimaryKey(record);
	}
	
	@Override
	public List<ProductImg> selectProImgs(ProductImg productImg) {
		// TODO Auto-generated method stub
		return productImgMapper.selectProImgs(productImg);
	}
	
	@Override
	@Transactional(rollbackFor = { Exception.class })
	public Result updateByProExtIdAndSysId(List<ProductImgVo> productImgs, Long proExtId, Long sysId, Long updateId) throws CustomException {
		Result result = new Result();
		// 假删除商品图片
		deleteByProExtIdAndSysId(proExtId, sysId);
		// 判断是否有主图
		boolean pcViewFlag = true;
		boolean wapViewFlag = true;
		// 更新商品图片
		if (productImgs == null || productImgs.size() == 0) {
			throw new CustomException(ErrorCode.ILLEGAL_PARAM.getCode(), "请设置商品图片");
		}
		for (ProductImgVo productImg : productImgs) {
			if (productImg.getPcViewTag().equals(ProductImgVo.YES_VIEW_TAG)) {
				pcViewFlag = false;
			}
			if (productImg.getWapViewTag().equals(ProductImgVo.YES_VIEW_TAG)) {
				wapViewFlag = false;
			}
			// 商品图片添加
			ProductImg img = new ProductImg();
			BeanUtils.copyProperties(productImg, img);
			img.setId(null);
			img.setProExtId(proExtId);
			img.setType(ProductImg.ALBUM);
			img.setSysId(sysId);
			img.setUpdateTime(DateUtils.getCurrentDateTime());
			img.setUpdateId(updateId);
			img.setDelFlag(false);
			img.setPcViewTag(productImg.getPcViewTag().equals(ProductImgVo.YES_VIEW_TAG));
			img.setWapViewTag(productImg.getWapViewTag().equals(ProductImgVo.YES_VIEW_TAG));
			insertSelective(img);
		}
		if (pcViewFlag && wapViewFlag) {
			throw new CustomException(ErrorCode.ILLEGAL_PARAM.getCode(), "请设置主图");
		}
		if (pcViewFlag) {
			throw new CustomException(ErrorCode.ILLEGAL_PARAM.getCode(), "请设置PC主图");
		}
		if (wapViewFlag) {
			throw new CustomException(ErrorCode.ILLEGAL_PARAM.getCode(), "请设置WAP主图");
		}
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setCnMessage("更新成功");
		return result;
	}
	
}
